(function( window ){
	
	var storageImage = function( obj, itemName ){
		this.obj = obj;
		this.itemName = itemName;
		this.image = new Image();
		this.init();
	};
	
	storageImage.prototype = {

		storage: function() {
			if(localStorage.getItem( this.itemName )) {
				this.obj.src = localStorage.getItem( this.itemName );
			} else {
				var canvas = document.createElement( "canvas" ),
					contxt = canvas.getContext( "2d" );
				
				//设置canvas和图片的宽高一样
				canvas.width = this.obj.width;
				canvas.height = this.obj.height;
				
				//渲染图片到canvas中
				canvas.drawImage( this.obj, 0, 0, this.width, this.height );
				
				var imageAsDataURL = canvas.toDataURL("image/png");
				
				//保存到本地中，因为存储有5M的大小限制，所以这里使用try catch 会更好
				try {
					localStorage.setItem( this.itemName, imageAsDataURL );
				} catch(e) {
					console.log(e);
				}
			}
		},
		
		init: function() {
			var _this = this;
			this.image.src = this.obj.src;
			this.image.addEventListener("load", function() {
				_this.storage();
			}, false);
		}
	};
	
	if( typeof define === "function" && define.amd ) {
		define( storageImage );
	} else {
		window.storageImage = storageImage;
	}
})( window )